var pkg_name="com.truedian.dragon";
Java.perform(function(){

    var ClassUse = Java.use("java.lang.Class");
    var objectclass= Java.use("java.lang.Object");
    
    var dexclassLoader = Java.use("dalvik.system.DexClassLoader");

    console.log("start==>1");
    var Dex=Java.use("com.android.dex.Dex");
    var param1 = Java.array('Ljava.lang.Class;',[]);
    console.log("start==>2");
    var Dex_getBytes=Dex.class.getDeclaredMethod("getBytes",param1);
    console.log("start==>3");
    var getDex=ClassUse.class.getDeclaredMethod("getDex",param1);
    console.log("start==>4");

    dexclassLoader.loadClass.overload('java.lang.String').implementation = function(name){
       
           var result = this.loadClass(name,false);
           //console.log("loadClass==>"+name);
            var hookClass = result;
            var hookClassCast = Java.cast(hookClass,ClassUse);
            var fileclass=Java.use("java.io.File");
            //start to dump
            var objArray=Java.array('Ljava.lang.Object;',[]);
            //console.log("start==>111");
            var myobj=getDex.invoke(hookClassCast,objArray);
            //console.log("start==>222");
            var  byteArray=Dex_getBytes.invoke(myobj,objArray);
           // console.log("start==>333");
            var  mysavePath="/data/data/"+pkg_name+"/mydex";
            //console.log("start==>444"+mysavePath);
            var  pathDir=fileclass.$new(mysavePath);
           
            if(!pathDir.exists())
            {
               pathDir.mkdirs();
            }
            //console.log("start==>555");
            var buffer = Java.array('byte',byteArray);
            //console.log(buffer)
            var length1=buffer.length;
            //console.log("length==========>"+length1);
            

            //save dir
            var file_path=mysavePath+"/dex_"+length1+".dex";
            var dexFile=fileclass.$new(file_path);
            if(!dexFile.exists())
            {
              console.log("###########################################");
              //dexFile.createNewFile();
              
              console.log("#####################################start to save############");
              var FileOutputStreamCls=Java.use("java.io.FileOutputStream");
              console.log("class for name:java.io.FileOutputStream");
              var FileOutputStreamObj=FileOutputStreamCls.$new(file_path);
              console.log("new object name:java.io.FileOutputStream");
              FileOutputStreamObj.write(buffer,0,length1);
              console.log("write ok############");
              FileOutputStreamObj.flush();
              FileOutputStreamObj.close();
              console.log("#####################################save ok############");
            }

            //release instance
            hookClassCast.$dispose();
            byteArray.$dispose();
            buffer.$dispose();
            pathDir.$dispose();
            fileclass.$dispose();
            //console.log("--------------------------------OVER--------------------------------");
            return result;

    }
});